package com.mogu_soup.util;

import com.mogu_soup.map.Brick;

import java.util.Vector;

/**
 * Version: 2.1
 * Author: TangJun
 * Date: 2024-08-03
 * Note:砖块对象池
 */
public class BrickPool {
    private static final int DEFAULT_SIZE = 200;//默认砖块池大小
    private static final int MAX_SIZE = 500;//
    private static Vector<Brick> pool;//砖块池
    //用静态代码块初始化，在类加载时就创建砖块对象
    static {
        pool = new Vector<Brick>(DEFAULT_SIZE);
        for (int i = 0; i < DEFAULT_SIZE; i++) {
            pool.add(new Brick(0,0, 0));
        }
    }

    //获取砖块池中对象
    public static synchronized Brick removeBrick() {
        return pool.isEmpty() ? new Brick(0,0, 0) : pool.remove(0);
    }

    //归还砖块池中对象
    public static synchronized void addBrick(Brick brick) {
        if (pool.size() < MAX_SIZE) {
            pool.add(brick);
        }
    }
}
